package cn.yuan.xiaoyu.guidemodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.activity.BaseActivity;
import cn.yuan.xiaoyu.commonmodule.adapter.PagerAdapter;
import cn.yuan.xiaoyu.commonmodule.manager.UserManager;
import cn.yuan.xiaoyu.commonmodule.utils.StatusBarCompatUtils;
import cn.yuan.xiaoyu.guidemodule.fragment.FirstGuideFragment;
import cn.yuan.xiaoyu.guidemodule.fragment.SecondGuideFragment;
import cn.yuan.xiaoyu.guidemodule.fragment.ThirdGuideFragment;
import cn.yuan.xiaoyu.mainmodule.activity.MainActivity;

/**
 * Created by yukuoyuan on 2017/2/27.
 * 这是一个引导界面
 */

public class GuideActivity extends BaseActivity {
    @Bind(R.id.vp_guide)
    ViewPager vpGuide;
    @Bind(R.id.bt_guide_start)
    Button btGuideStart;
    @Bind(R.id.tv_guide_first_check)
    ImageView tvGuideFirstCheck;
    @Bind(R.id.tv_guide_second_check)
    ImageView tvGuideSecondCheck;
    @Bind(R.id.tv_guide_third_check)
    ImageView tvGuideThirdCheck;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void bindPresenter() {

    }

    @Override
    protected void initData(Bundle extras) {
        fragments.add(new FirstGuideFragment());
        fragments.add(new SecondGuideFragment());
        fragments.add(new ThirdGuideFragment());
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);
        vpGuide.setAdapter(pagerAdapter);
        vpGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2) {
                    btGuideStart.setVisibility(View.VISIBLE);
                } else {
                    btGuideStart.setVisibility(View.GONE);
                }
                clearstatus();
                switch (position) {
                    case 0:
                        tvGuideFirstCheck.setBackgroundResource(R.drawable.bg_guide_check);
                        break;
                    case 1:
                        tvGuideSecondCheck.setBackgroundResource(R.drawable.bg_guide_check);
                        break;
                    case 2:
                        tvGuideThirdCheck.setBackgroundResource(R.drawable.bg_guide_check);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 清楚选中状态
     */
    public void clearstatus() {
        tvGuideFirstCheck.setBackgroundResource(R.drawable.bg_guide_uncheck);
        tvGuideSecondCheck.setBackgroundResource(R.drawable.bg_guide_uncheck);
        tvGuideThirdCheck.setBackgroundResource(R.drawable.bg_guide_uncheck);

    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
    }

    @OnClick({R.id.bt_guide_start})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_guide_start:
                UserManager.setIsFirst(true);
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
